package aom.ju.ss.view.user.shop.management.goods

import android.os.Bundle
import android.os.Message
import android.view.View
import androidx.recyclerview.widget.LinearLayoutManager
import aom.ju.ss.R
import aom.ju.ss.databinding.ListBinding
import aom.ju.ss.databinding.ShopManngerItemBinding
import aom.ju.ss.lpInterface.MyHttpCallBack
import aom.ju.ss.lpInterface.TitleDialogInterface
import aom.ju.ss.mode.LpKey
import aom.ju.ss.mode.ShopManngerGoodsList
import aom.ju.ss.mode.ShopManngerGoodsListData
import aom.ju.ss.utils.*
import aom.ju.ss.utils.http.LP_API
import aom.ju.ss.utils.http.OKHttpUtils
import aom.ju.ss.view.base.BaseFragment
import aom.ju.ss.view.base.PAdapter
import aom.ju.ss.view.cut.CutGoodsDetailsActivity
import aom.ju.ss.view.dialog.TitleDialog
import com.google.gson.Gson

class GoodsManageListFragment : BaseFragment() {

    companion object {
        fun getMys(u: String): GoodsManageListFragment {
            val f = GoodsManageListFragment()
            val bu = Bundle()
            bu.putString("type", u)
            f.arguments = bu
            return f
        }
    }

    var type = ""
    var list = arrayListOf<ShopManngerGoodsListData>()
    var page = 1
    private lateinit var padapter: PAdapter

    private lateinit var binding: ListBinding
    override fun init() {
        type = requireArguments().getString("type", "")
        binding = viewBinding as ListBinding
        binding.rootView.setBackgroundResource(R.color.color_f5f5f5)
        padapter = PAdapter(
            list, R.layout.shop_mannger_item
        ) { b, position ->
            val ib = b as ShopManngerItemBinding
            val info = list[position]
            ib.info = info
            ib.edit.setOnClickListener { }

            ib.changeInventory.setOnClickListener {
                start(
                    ChangeInventory::class.java,
                    info.id.toString()
                )
            }
            ib.offShelf.text = if (type == "3") "上架" else "下架"
            ib.offShelf.setOnClickListener {
                TitleDialog().show(activity, object : TitleDialogInterface {
                    override fun ok(view: View?, IsDetermine: Boolean) {
                        if (IsDetermine) {
                            OKHttpUtils(activity, false)
                                .SetApiUrl(LP_API.save_status)
                                .SetKey("id", "status")
                                .SetValue(info.id.toString(), if (type == "3") "1" else "0")
                                .POST(object : MyHttpCallBack {
                                    override fun ok(jsonString: String?, httpTY: String?) {
                                        if (type == "3") {
                                            "上架成功!".show
//                                            val offShelf = 7//下架成功
//                                            val putOnTheShelf = 8//上架成功
                                            SendMsg.sendEvent(LpKey.putOnTheShelf)
                                        } else {
                                            "下架成功!".show
                                            SendMsg.sendEvent(LpKey.offShelf)
                                        }
                                    }

                                    override fun error(
                                        e: String?,
                                        jsonString: String?,
                                        httpTY: String?
                                    ) {
                                        e?.show
                                    }

                                })
                        }
                    }
                }).setConnect(
                    if (type == "3") "确定上架?" else
                        "  下架后，买家将无法在聚刷刷搜索以及查看本商品，确定下架？"
                ).tvCancel("取消")
                    .tvDetermine(if (type == "3") "上架" else "下架")
            }//下架
            ib.look.setOnClickListener {
                start(
                    CutGoodsDetailsActivity::class.java,
                    info.id.toString()
                )
            }
        }
        binding.mRecycler.layoutManager = LinearLayoutManager(activity)
        binding.mRecycler.adapter = padapter
        data
        binding.small.setOnRefreshListener {
            page = 1
            data
        }
        binding.small.setOnLoadMoreListener {
            page++
            data
        }

//        binding.ss.addTextChangedListener(object : TextWatcherLP() {
//            override fun afterTextChanged(s: Editable?) {
//                super.afterTextChanged(s)
//                data
//            }
//        })
    }

    override fun callBack(event: Message?) {
        super.callBack(event)
        event?.let {
            if (it.arg1 == LpKey.offShelf) {//下架
                page = 1
                data
            } else if (it.arg1 == LpKey.putOnTheShelf) {//上架成功
                page = 1
                data
            }
        }
    }

    var ok = OKHttpUtils(activity, false)
    val data: Unit
        get() {
            ok = OKHttpUtils(activity, false)
            ok.SetApiUrl(LP_API.shop_goods_get_list)
                .SetKey("keyword", "page", "status")
                .SetValue(GoodsManage.ss, page.toString(), type)
                .POST(this)
        }

    override fun ok(jsonString: String?, httpTY: String?) {
        super.ok(jsonString, httpTY)
        val mode = Gson().fromJson(jsonString, ShopManngerGoodsList::class.java)
        binding.small.Fin()
        if (page == 1) {
            list.clear()
        }
        mode.data?.let {
            list.addAll(it)
        }
        padapter.notifyDataSetChanged()
        binding.noData.VG(list.size == 0)
    }

    override fun error(e: String?, jsonString: String?, httpTY: String?) {
        super.error(e, jsonString, httpTY)
        binding.small.Fin()
        e?.show
    }

    override fun getLay(): Int {
        return R.layout.list
    }
}